// const OSS = require('');
import OSS from 'ali-oss';
import { useResourceApi } from '/@/api/resource';

const { saveUpload } = useResourceApi();

let client = new OSS({
	accessKeyId: 'LTAI5tF9tEnSJ8SE6BxDe4sS',
	accessKeySecret: 'PSFQSdRDx9rJ9A8jybmyD4Jlf2VNlL',
	bucket: 'st-future',
	region: 'oss-cn-hongkong',
	secure: true,
});
let type = {
	shopimg: 0,
	shopvideo: 1,
	shoppdf: 2,
};
const uploadOss = (options, path) => {
	try {
		const file = options.file;
		const fileName = file.name;
		let date = new Date().getTime();
		let fileNames = `${date}_${fileName}`;
		return new Promise(() => {
			client.put(path + '/' + fileNames, file).then((res) => {
				const { name } = res;
				const { statusCode, requestUrls } = res.res;
				if (statusCode === 200) {
					requestUrls.forEach((item) => {
						let params = {
							picName: name,
							picUrl: item,
							type: type[path],
						};
						saveUpload(params);
					});
					options.onSuccess(res);
				}
			});
		});
	} catch (err) {
		options.onError('上传失败');
	}
};

export default uploadOss;
